$(function () {
    layui.use(['form', 'layer', 'laydate'], function () {
        $ = layui.jquery;
        form = layui.form;
        // 全选/取消
        form.on('checkbox(c_all)', function (data) {
            var a = data.elem.checked;
            if (a === true) {
                $("input[type='checkbox']").prop("checked", true);
                form.render('checkbox');
            } else {
                $("input[type='checkbox']").prop("checked", false);
                form.render('checkbox');
            }
        });
    });
    var conceal= $('.conceal');
    try {
        conceal.parent('div').find('input').attr('type','password')
    }catch (e) {
        console.debug('未获取的到元素')
    }
    conceal.on('click', function () {
        var status = $(this).attr('class');
        if(status.includes('fa-eye-slash')){
            $(this).removeClass('fa-eye-slash');
            $(this).addClass('fa-eye');
            $(this).parent('div').find('input').attr('type','text')
        }else {
            $(this).removeClass('fa-eye');
            $(this).addClass('fa-eye-slash');
            $(this).parent('div').find('input').attr('type','password')
        }
    });
});

//form_提交_POST
function submit_post(location = null, form_id = null) {
    var data = $("form").serializeArray();
    if (form_id) {
        data = $(form_id).serializeArray();
    }
    $.ajax({
        type: 'POST',
        data: data,
        dataType: "json",
        success: function (data) {
            if (data.status === 0) {
                layer.msg('操作成功', {icon: 1, time: 2000});
                try {
                    var index = parent.layer.getFrameIndex(window.name);
                    window.parent.location.reload();//刷新父页面
                    parent.layer.close(index);
                } catch (e) {
                }
                if (location) {
                    window.location.href = location;
                }
            } else if (data.status === -1) {
                layer.msg('操作警告: ' + data.msg, {icon: 7, time: 3000});
            } else {
                layer.msg('操作失败: ' + data.msg, {icon: 2, time: 3000});
            }
        }, error: function (data) {
            layer.msg('操作失败!', {icon: 7, time: 2000});
        }
    });
    return status;
}

// 删除_DELETE
function del(obj, id) {
    layer.confirm('确认要删除吗？', function (index) {
        $.ajax({
            type: "DELETE",
            data: {"uid": id},
            dataType: "json",
            success: function (data) {
                if (data.status === 0) {
                    $(obj).parents("tr").remove();
                    layer.msg('删除成功!', {icon: 1, time: 1000});
                } else {
                    layer.msg('删除失败: ' + data.msg, {icon: 2, time: 2000})
                }
            }, error: function (data) {
                layer.msg('操作异常!', {icon: 7, time: 2000});
            }
        });
    });
    return false;
}


// 其他_other
function other(obj, id, title, method,is_not_remove=true) {
    layer.confirm('确认要' + title + '吗？', function (index) {
        $.ajax({
            type: method,
            data: {"data": id},
            dataType: "json",
            success: function (data) {
                if (data.status === 0) {
                    if(is_not_remove){$(obj).parents("tr").remove();}
                    layer.msg(title + '成功!', {icon: 1, time: 1000});
                    setTimeout(function () {
                        window.location.reload();//页面刷新
                    }, 2000);
                } else {
                    layer.msg(title + '失败: ' + data.msg, {icon: 2, time: 2000});
                }
            }, error: function (data) {
                layer.msg('操作异常!', {icon: 7, time: 2000});
            }
        });
    });
    return false;
}


// 数据恢复
function recover(obj, id) {
    layer.confirm('确认要恢复吗？', function (index) {
        $.ajax({
            type: "RECOVER",
            data: {"uid": id},
            dataType: "json",
            success: function (data) {
                if (data.status === 0) {
                    $(obj).parents("tr").remove();
                    layer.msg('恢复成功!', {icon: 1, time: 1000});
                } else {
                    layer.msg('恢复失败: ' + data.msg, {icon: 2, time: 2000});
                }
            }, error: function (data) {
                layer.msg('操作异常!', {icon: 7, time: 2000});
            }
        });
    });
    return false;
}

// 弹出上传文件窗口
function open_file() {
    document.getElementById('upload').click();
}


//图片上传
function upload_img(url_up, el_id) {
    var formData = new FormData();
    formData.append('file', $('#upload')[0].files[0]);  //添加图片信息的参数
    $.ajax({
        url: url_up,
        type: 'POST',
        cache: false, //上传文件不需要缓存
        data: formData,
        processData: false, // 告诉jQuery不要去处理发送的数据
        contentType: false, // 告诉jQuery不要去设置Content-Type请求头
        success: function (data) {
            if (data.status === 0) {
                $(el_id).val(data.img_url);
            } else {
                layer.msg(data.msg, {icon: 2, time: 2000});
            }
        },
        error: function (data) {
            layer.msg('上传失败!');
        }
    });
    return false;
}


// 批量操作
function checkbox(obj, method) {
    var id_array = Array();
    $('input[type="checkbox"]:checked').each(function () {
        id_array.push($(this).val());
    });
    id_array = id_array.join('#TL#');
    if (id_array !== 'on' && id_array !== '') {
        layer.confirm('确认要批量操作吗？', function (index) {
            $.ajax({
                type: method,
                data: {"id_array": id_array},
                dataType: "json",
                success: function (data) {
                    if (data.status === 0) {
                        layer.msg('操作成功!', {icon: 1, time: 1000});
                        location.reload()
                    } else {
                        layer.msg('操作失败:' + data.msg, {icon: 2, time: 2000});
                    }
                },
                error: function (data) {
                    layer.msg('操作异常!', {icon: 7, time: 2000});
                }
            });
        })
    } else {
        layer.msg('请选择操作对象!', {icon: 2, time: 2000});
    }
}

function subMethod(formName, method, url) {
    var data = $("form[name=" + formName + "]").serializeArray();
    $.ajax({
        url: url,
        type: method,
        data: data,
        dataType: "json",
        success: function (data) {
            if (data.status === 0) {
                layer.msg(data.msg, {icon: 1});
            } else {
                layer.msg(data.msg, {icon: 2});
            }
        }, error: function () {
            layer.msg('系统操作异常', {icon: 7});
        }
    })
}

function install_plug(obj, id) {
    layer.confirm('确认要安装插件吗？', function (index) {
        layer.msg('正在加载...（耗时1-10分钟，请勿刷新页面）', {icon: 16,shade: 0.3,time:false});
        $.ajax({
            type: 'PUSH',
            data: {"data": id},
            dataType: "json",
            success: function (data) {
                if (data.status === 0) {
                    layer.msg('正在安装...（耗时1-10分钟，请勿刷新页面）', {icon: 16,shade: 0.3,time:false});
                    $.ajax({
                            type: 'INSTALL',
                            data: {"data": id},
                            dataType: "json",
                            success: function (data) {
                                if (data.status === 0) {
                                    layer.msg('安装插件成功!', {icon: 1, time: 1000});
                                    setTimeout(function () {
                                        window.location.reload();
                                    }, 2000);
                                } else {
                                    layer.msg('安装失败: ' + data.msg, {icon: 2, time: 2000});
                                }
                            }, error: function (data) {
                                layer.msg('安装异常!', {icon: 7, time: 2000});
                            }
                        });
                } else {
                    layer.msg('加载失败: ' + data.msg, {icon: 2, time: 2000});
                }
            }, error: function (data) {
                layer.msg('加载异常!', {icon: 7, time: 2000});
            }
        });
    });
    return false;
}

function btn_status(obj) {
    var datatype = $(obj).attr('datatype');
    var id_array = Array();
    $('input[type="checkbox"]:checked').each(function () {
        id_array.push($(this).val());
    });
    id_array = id_array.join('#TL#');
    if (id_array !== 'on' && id_array !== '') {
        layer.confirm('确认要批量操作吗？', function (index) {
            $.ajax({
                type: 'UPDATE',
                data: {"id_array": id_array,"datatype":datatype},
                dataType: "json",
                success: function (data) {
                    if (data.status === 0) {
                        layer.msg('操作成功!', {icon: 1, time: 1000});
                        setTimeout(function () {location.reload()}, 2000);
                    } else {
                        layer.msg('操作失败:' + data.msg, {icon: 2, time: 2000});
                    }
                },
                error: function (data) {
                    layer.msg('操作异常!', {icon: 7, time: 2000});
                }
            });
        })
    } else {
        layer.msg('请选择操作对象!', {icon: 2, time: 2000});
    }

}